package com.hutao.manage.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hutao.manage.datasource.TargetDataSource;
import com.hutao.manage.mapper.MedicalPrinterRangeMapper;
import com.hutao.manage.mapper.MedicalRecordRetrievalMapper;
import com.hutao.manage.mapper.TreatmentTraceMapper;
import com.hutao.manage.query.MedRecordQuery;
import com.hutao.manage.query.MedicalSearchQuery;
import com.hutao.manage.service.MedicalRecordRetrievalService;
@Service
public class MedicalRecordRetrievalServiceImpl implements MedicalRecordRetrievalService {

	@Autowired
	private MedicalRecordRetrievalMapper medicalRecordRetrievalMapper;
	
	@Autowired
	private TreatmentTraceMapper treatmentTraceMapper;
	
	@Autowired
	private MedicalPrinterRangeMapper medicalPrinterRangeMapper;
	
	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public List<Map<String, Object>> selectMedicalRecordRetrievaList(MedicalSearchQuery medicalSearchQuery) {
		return medicalRecordRetrievalMapper.selectMedicalRecordRetrievaList(medicalSearchQuery);
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public List<Map<String, Object>> selectUnScanMedicalRecordList(MedicalSearchQuery medicalSearchQuery) {
		return medicalRecordRetrievalMapper.selectUnScanMedicalRecordList(medicalSearchQuery);
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public List<Map<String, Object>> selectScanMedicalRecordList(MedicalSearchQuery medicalSearchQuery) {
		return medicalRecordRetrievalMapper.selectScanMedicalRecordList(medicalSearchQuery);
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public int getUploadScanByFirstGuid(String firstGuid) {
		return medicalRecordRetrievalMapper.getUploadScanByFirstGuid(firstGuid);
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public List<Map<String, Object>> ajaxQualityEvaluationPage(MedRecordQuery params) {
		
		return medicalRecordRetrievalMapper.ajaxQualityEvaluationPage(params);
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public long ajaxQualityEvaluationPageTotalCount(MedRecordQuery params) {
		return medicalRecordRetrievalMapper.ajaxQualityEvaluationPageTotalCount(params);
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public Map<String, Object> getMedRecordByFirstGuid(String firstGuid) {
		return medicalRecordRetrievalMapper.getMedRecordByFirstGuid(firstGuid);
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public List<Map<String, Object>> ajaxFirstQualityEvaluationPage(MedRecordQuery params) {
		
		return medicalRecordRetrievalMapper.ajaxFirstQualityEvaluationPage(params);
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public long ajaxFirstQualityEvaluationPageTotalCount(MedRecordQuery params) {
		return medicalRecordRetrievalMapper.ajaxFirstQualityEvaluationPageTotalCount(params);
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public Map<String, Object> getMedRecordQualityRate(MedRecordQuery params) {
		Map<String,Object> map= new HashMap<>();
		Map<String,Object> totalQualityMap=new HashMap<>();
		List<Map<String,Object>> officeQualityRateList = new ArrayList<>();
		List<Map<String,Object>> outOfficeList = medicalRecordRetrievalMapper.getOutOfficeQualityRate(params);
		double totalQualityRate=0;
		//数据为空直接返回
		if(CollectionUtils.isEmpty(outOfficeList)){
			return map;
		}

		//质控总数
		params.setTreamentTraceTypeId(3);
		int totalQualityCount = medicalRecordRetrievalMapper.getQualityCount(params);
		
		//出院总数
		params.setTreamentTraceTypeId(2);
		int totalOutCount = medicalRecordRetrievalMapper.getQualityCount(params);
		
		
		if(totalOutCount>0){
			totalQualityRate =truncatePoint(((double)totalQualityCount/(double)totalOutCount)*100);
		}
		
		totalQualityMap.put("totalOutCount", totalOutCount);
		totalQualityMap.put("totalQualityCount", totalQualityCount);
		totalQualityMap.put("totalQualityRate", totalQualityRate);
		
		
//		{officeName=呼吸内科, outOfficeCount=2, outDeptId=1}
//		{officeName=外科, outOfficeCount=1, outDeptId=2}
		for (Map<String, Object> outOffice : outOfficeList) {
			double qualityRate=0;
			//数据为空跳出此次循环执行下次
			if(MapUtils.isEmpty(outOffice)){
				continue;
			}
			//出院数量
			long outOfficeCount=outOffice.get("outOfficeCount")==null?0:(long)outOffice.get("outOfficeCount");
			params.setOutHospitalOfficeId((Integer)outOffice.get("outDeptId"));
			params.setTreamentTraceTypeId(3);
			int qualityCount = medicalRecordRetrievalMapper.getQualityCount(params);
			outOffice.put("qualityCount", qualityCount);
			if(outOfficeCount>0){
				qualityRate =truncatePoint(((double)qualityCount/(double)outOfficeCount)*100);
			}
			outOffice.put("qualityRate", qualityRate);
			officeQualityRateList.add(outOffice);
			
		}
		
		map.put("totalQualityMap", totalQualityMap);
		map.put("officeQualityRateList", officeQualityRateList);
		return map;
	}


    
    public static double truncatePoint(double num){
    	BigDecimal   decinal = new BigDecimal(num);  
    	return (double) decinal.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();  
    }

    @TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public List<Map<String, Object>> getOutHospitalNumByOfficeAndOutTime(MedRecordQuery medRecordQuery) {
		
		return medicalRecordRetrievalMapper.getOutHospitalNumByOfficeAndOutTime(medRecordQuery);
	}

    @TargetDataSource(name="ds1")
   	@Transactional(readOnly=true)
	@Override
	public List<Map<String, Object>> getTreatmentTraceByCodeAndGuid(Integer code, String guid) {
		
		return treatmentTraceMapper.getTreatmentTraceByCodeAndGuid(code, guid);
	}

    @TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public List<Integer> getPrintPageByPrintType(Integer printTypeId) {
		return medicalPrinterRangeMapper.getPrintPageByPrintType(printTypeId);
	}
    
    @Transactional(readOnly=true)
	@Override
    public int selectMedApplyCount() {
    	return medicalRecordRetrievalMapper.selectMedApplyCount();
    }
    
	


}
